package org.yaac.server.service;

import java.util.List;

import org.yaac.client.service.OverviewService;
import org.yaac.shared.cap.CapSummaryDTO;
import org.yaac.shared.cap.CapSummaryDTO.Status;

import com.google.appengine.api.capabilities.CapabilitiesService;
import com.google.appengine.api.capabilities.CapabilitiesServiceFactory;
import com.google.appengine.api.capabilities.Capability;
import com.google.appengine.api.capabilities.CapabilityStatus;
import com.google.common.collect.ImmutableList;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class OverviewServiceImpl implements OverviewService {

	private static final List<Capability> caps = ImmutableList.<Capability>builder(
			).add(Capability.BLOBSTORE)
			.add(Capability.DATASTORE)
			.add(Capability.DATASTORE_WRITE)
			.add(Capability.IMAGES)
			.add(Capability.MAIL)
			.add(Capability.MEMCACHE)
			.add(Capability.TASKQUEUE)
			.add(Capability.URL_FETCH)
			.add(Capability.XMPP)
			.build();
	
	@Override
	public CapSummaryDTO loadCapSummary() {
		CapabilitiesService service = CapabilitiesServiceFactory.getCapabilitiesService();
		
		CapSummaryDTO result = new CapSummaryDTO();
		for (Capability cap : caps) {
			CapabilityStatus status = service.getStatus(cap).getStatus();
			
			switch(status) {
			case ENABLED:
				result.setStatus(cap.getPackageName(), cap.getName(), Status.ENABLED);
				break;
			case SCHEDULED_MAINTENANCE:
				result.setStatus(cap.getPackageName(), cap.getName(), Status.SCHEDULED_MAINTENANCE);
				break;
			case DISABLED:
				result.setStatus(cap.getPackageName(), cap.getName(), Status.DISABLED);
				break;
			case UNKNOWN:
				result.setStatus(cap.getPackageName(), cap.getName(), Status.UNKNOWN);
				break;
			}
		}

		return result;
	}
}
